package app.web.cartoon.report.num.tool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.lang.BusinessDate;

/**
 * 基本数值统计类
 * @author JiaJ
 *
 */
public abstract class BaseNumReport{
	
	/**
	 * 数据类型
	 */
	private String dataTyp = "";
	
	/**
	 * 原始数据
	 */
	private Map<String, Integer> originMap = new HashMap<String, Integer>();
	
	/**
	 * 日期顺序
	 */
	private List<String> dateList = new ArrayList<String>();
	
	/**
	 * 数据总量变化
	 */
	protected Map<String, Integer> dataAllMap = new HashMap<String, Integer>();
	
	/**
	 * 数据增量变化
	 */
	protected Map<String, Integer> dataPlusMap = new HashMap<String, Integer>();
	
	/**
	 * 最终日期
	 */
	protected List<String> dateResult = new ArrayList<String>();
	
	/**
	 * 设置数据类
	 * @param dataTyp
	 */
	public void setDataTyp(String dataTyp){
		this.dataTyp = dataTyp;
	}
	
	/**
	 * 初始化方法
	 */
	public void inst(){
		
		this.beforeData();
		
		//初始数据装载
		this.instData();
		
		//增量数据分析
		this.analyseSkipData();
		
		//总体数据分析
		this.analyseTotalData();
		
		//数据后续处理
		this.afterData();
	}
	
	protected abstract void afterData();
	
	protected abstract void beforeData();

	/**
	 * 初始数据装载
	 */
	protected void instData(){
		
		this.dateList.clear();
		this.originMap.clear();
		
		String sql = "select to_char(RES_TIME,'yyyy-mm-dd') RES_TIME,RES_ID from LEO_RES_INFO ";	
		if(!"".equals(dataTyp)){
			sql += " where RES_TYP='"+this.dataTyp+"' ";
		}		
		sql += " order by RES_TIME ";
	
		DataTable dt = DBFactory.getActionDB().executeQueryTable(sql);
		
		while(dt.next()){			
			this.plusSkip(dt.getString("RES_TIME"));		
		}
		
	}
	
	/**
	 * 数据分析
	 */
	protected void analyseSkipData(){
		
		//遍历日期
		for(String date:this.dateList){
			
			this.dataPlusMap.put(date, this.originMap.get(date));
			
			this.dateResult.add(date);
			
			this.plusDate(date);	
			
		}
		
	}
	
	/**
	 * 总数分析
	 */
	protected void analyseTotalData(){
		
		for(String date:this.dateResult){
			
			String preDate = BusinessDate.plusDates(date, 0, 0, -1);
			
			if(this.dataAllMap.containsKey(preDate)){
				
				int total = this.dataAllMap.get(preDate);
				int skip = this.dataPlusMap.get(date);
				this.dataAllMap.put(date, total+skip);
				
			}else{
				
				int skip = this.dataPlusMap.get(date);
				this.dataAllMap.put(date, skip);
				
			}
			
		}
		
	}
	
	/**
	 * 装载递增数据
	 * @param date
	 */
	protected void plusSkip(String date){		
		if(originMap.containsKey(date.trim())){			
			int active = this.originMap.get(date);
			this.originMap.put(date, active+1);		
		}else{
			this.dateList.add(date);
			this.originMap.put(date, 1);
		}		
	}
	
	/**
	 * 添加空余日期
	 * @param date
	 */
	protected void plusDate(String date){	
		
		if(date.equals(BusinessDate.getToday())){
			return;
		}
		
		String activeDate = BusinessDate.plusDates(date, 0, 0, 1);
		if(this.dateList.contains(activeDate)){
			return;
		}else{			
			this.dataPlusMap.put(activeDate, 0);
			this.dateResult.add(activeDate);
			this.plusDate(activeDate);		
		}
		
		
	}
	
}
